package com.wavehi.web;

import com.wavehi.pojo.User;
import com.wavehi.service.UserService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author WaveHi
 * @Date 19/9/2021 下午 2:16
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    //获取service层对象
    private UserService us= new UserService();

    /**
     * 无实际意义，仅供开发使用
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void base(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    /**
     * 自动登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void loginByCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取cookie信息
        Cookie[] cookies = req.getCookies();
        String username = null;
        String password = null;
        for(Cookie cookie : cookies) {
            System.out.println("Path :  " + cookie.getPath());
            if("password".equalsIgnoreCase(cookie.getName())) {
                password = cookie.getValue();
            }
            if("username".equalsIgnoreCase(cookie.getName())) {
                username = cookie.getValue();
            }
        }
        //验证信息
        boolean isCorrect = us.userExists(username, password);
        //转发相应的页面
        if(isCorrect) {
            //设置响应信息
            req.setAttribute("correctMsg", "Login completed!");
            //获取请求转发对象
            RequestDispatcher dis = req.getRequestDispatcher("page/user/user_index.jsp");
            //转发
            dis.forward(req, resp);
        }else {
            //设置响应信息
            req.setAttribute("errorMsg", "Please login in again!");
            //获取请求转发对象
            RequestDispatcher dis = req.getRequestDispatcher("page/user/user_login.jsp");
            //转发
            dis.forward(req, resp);
        }
    }

    /**
     * 注册
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取注册信息
        String username = req.getParameter("username").trim();
        String password = req.getParameter("password").trim();
        //判断用户是否已存在
        boolean exists = false;
        exists = us.userExists(username);
        //向数据库添加信息
        if(!exists) {
            User user = new User(username, password);
            us.add(user);
            //设置cookie信息
            Cookie usernameCookie = new Cookie("username", username);
            Cookie passwordCookie = new Cookie("password", password);
            passwordCookie.setMaxAge(60 * 60 * 24 * 7);
            usernameCookie.setMaxAge(60 * 60 * 24 * 7);
            passwordCookie.setPath("/");
            usernameCookie.setPath("/");
            //将cookie添加到回传数据中
            resp.addCookie(usernameCookie);
            resp.addCookie(passwordCookie);
            //获取主页面的转发对象
            RequestDispatcher dis = req.getRequestDispatcher("page/user/user_index.jsp");
            //转发
            dis.forward(req, resp);
        }else {
            //设置响应信息
            req.setAttribute("ErrorMsg", "The user really have existed!");
            //获取转发对象
            RequestDispatcher dis = req.getRequestDispatcher("page/user/user_register.jsp");
            //转发
            dis.forward(req, resp);
        }
    }

    /**
     * 登录
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取登录信息
        String username = req.getParameter("username").trim();
        String password = req.getParameter("password").trim();
        //判断登录信息是否正确
        boolean isCorrect = us.userExists(username,password);
        //转发相应的页面
        if(isCorrect) {
            //设置响应信息
            req.setAttribute("correctMsg", "Login completed!");
            //设置cookie信息
            Cookie usernameCookie = new Cookie("username", username);
            Cookie passwordCookie = new Cookie("password", password);
            passwordCookie.setMaxAge(60 * 60 * 24 * 7);
            usernameCookie.setMaxAge(60 * 60 * 24 * 7);
            passwordCookie.setPath("/");
            usernameCookie.setPath("/");
            //将cookie添加到回传数据中
            resp.addCookie(usernameCookie);
            resp.addCookie(passwordCookie);
            //获取请求转发对象
            RequestDispatcher dis = req.getRequestDispatcher("page/user/user_index.jsp");
            //转发
            dis.forward(req, resp);
        }else {
            //设置响应信息
            req.setAttribute("errorMsg", "Username or password is error!");
            //获取请求转发对象
            RequestDispatcher dis = req.getRequestDispatcher("page/user/user_login.jsp");
            //转发
            dis.forward(req, resp);
        }
    }


}
